package com.mao.controller;

import com.mao.listenner.MyHttpSessionListener;


import com.mao.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/*
user：mao
data：2021/1/31
note：
*/
//这个注解相当于ResponseBody+Controller
//@RestController

@Controller
public class HelloController {

    @RequestMapping("/hello")
    //只有在这里面使用了Session对象，才会创建Session对象，触发对应事件。
    public String sayHello(HttpSession session, Model model){
        model.addAttribute("msg","this from thymeleaf");
        session.setAttribute("username","longmao");
        return "hello";
    }

    @RequestMapping("/thymeleaf")
    public String thymeleaf(ModelMap map){

        map.put("thText","th:text设置文本内容 <b>加粗</b>");
        map.put("thUText","th:utext 设置文本内容 <b>加粗</b>");
        map.put("thValue","thValue 设置当前元素的value值");
        map.put("thEach","Arrays.asList(\"th:each\", \"遍历列表\")");
        map.put("thIf","msg is not null");
        map.put("thObject",new Person("zhangsan","男",12));
        return "thymeleaf";
    }

    //验证如果试图解析器里面路径和Controller的路径重合时走哪一个
    //显然是走Controller.
    @RequestMapping("login")
    public String login(){
        System.out.println("in the controller");
        return "login";
    }


    @RequestMapping("/online")
    public String online(){
        return "在线人数："+ MyHttpSessionListener.online;
    }
}
